package com.leoman.controller.api;

import com.leoman.core.ErrorType;
import com.leoman.core.bean.Result;
import com.leoman.entity.Broadcast;
import com.leoman.exception.ParamErrorException;
import com.leoman.service.BroadcastService;
import com.leoman.utils.JsonUtil;
import com.leoman.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 广播api
 * Created by 涂奕恒 on 2016/11/25 0018.
 */
@Controller
@RequestMapping(value = "api/broadcast")
public class BroadcastApi {

    @Autowired
    private BroadcastService broadcastService;

    /**
     * @api {post} /api/broadcast/list 1、广播列表
     * @apiVersion 0.0.1
     * @apiName broadcast.list
     * @apiGroup broadcast
     *
     * @apiDescription 广播列表
     *
     * @apiParam   {Integer}   userId                     用户Id
     * @apiParam   {Integer}   isMe                       是否是我发的，0=否，1=是，默认为0
     * @apiParam   {Integer}   pageNum                    页码
     * @apiParam   {Integer}   pageSize                   每页显示条数
     *
     * @apiSuccess {Object}    list
     * @apiSuccess {Integer}   list.id                    广播ID
     * @apiSuccess {Integer}   list.title                 标题
     * @apiSuccess {String}    list.url                   语音url
     * @apiSuccess {String}    list.duration              语音长度
     * @apiSuccess {Integer}   list.isAdmin               是否管理员发送，0=否，1=是
     * @apiSuccess {Integer}   list.isTop                 是否置顶，0=否，1=是
     * @apiSuccess {Integer}   list.praiseNum             点赞数
     * @apiSuccess {Integer}   list.commentNum            评论数
     * @apiSuccess {Integer}   list.collectNum            收藏数
     * @apiSuccess {Integer}   list.isPraise              是否点赞，0=否，1=是
     * @apiSuccess {Integer}   list.isComment             是否评论，0=否，1=是
     * @apiSuccess {Integer}   list.isCollect             是否收藏，0=否，1=是
     * @apiSuccess {Integer}   list.userId                发送人ID
     * @apiSuccess {String}    list.nickName              发送人昵称
     * @apiSuccess {String}    list.avatar                发送人头像
     * @apiSuccess {Integer}   list.regionId              发送人所在地ID
     * @apiSuccess {Integer}   list.regionInfo            发送人所在地名称
     * @apiSuccess {String}    list.createTime            发送时间
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *      "status":0,
     *      "msg":"",
     *      "data":{
     *          "page": {
     *              "totalNum": 0,
     *              "totalPage": 0,
     *              "currentPage": 1
     *          },
     *          "list": [
     *              {
     *                  "id" : 1,
     *                  "title" : 1,
     *                  "url" : "http://4.16.3.22:8080/images/cover.jpg",
     *                  "duration" : 32,
     *                  "isAdmin" : 0,
     *                  "isTop" : 1,
     *                  "praiseNum" : 124,
     *                  "commentNum" : 20,
     *                  "collectNum" : 20,
     *                  "isPraise" : 0,
     *                  "isComment" : 0,
     *                  "isCollect" : 0,
     *                  "userId" : 1,
     *                  "nickName" : "张三",
     *                  "avatar" : "http://4.16.3.22:8080/images/cover.jpg",
     *                  "regionId" : 1,
     *                  "regionInfo" : 1,
     *                  "createTime" : "1476843673494"
     *              }
     *          ]
     *      }
     * }
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public void list(HttpServletResponse response, Integer userId, Integer isMe, Integer pageNum, Integer pageSize) {
        Page<Broadcast> page = broadcastService.iPageForApp(userId, isMe, pageNum, pageSize);
        Map<String, Object> dataMap = JsonUtil.fitting(page);
        String result = JsonUtil.obj2ApiJson(Result.successApi(dataMap), "user");
        WebUtil.printApi(response, result);
    }

    /**
     * @api {post} /api/broadcast/save 2、添加广播
     * @apiVersion 0.0.1
     * @apiName broadcast.save
     * @apiGroup broadcast
     *
     * @apiDescription 添加广播
     *
     * @apiParam   {Integer}   userId                用户Id                   （必填）
     * @apiParam   {String}    title                 标题                     （必填）
     * @apiParam   {Stream}    voice                 语音                     （必填）
     * @apiParam   {Integer}   duration              时长                     （必填）
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *      "status":0,
     *      "msg":"",
     *      "data":{}
     * }
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public void save(HttpServletResponse response, Integer userId, String title, Integer duration, MultipartRequest multipartRequest) {
        try {
            broadcastService.addBroadcastInfo(false, userId, title, duration, multipartRequest);
            String result = JsonUtil.obj2ApiJson(Result.successApi());
            WebUtil.printApi(response, result);
        } catch (ParamErrorException e) {
            WebUtil.printApi(response, Result.failureApi(ErrorType.ERROR_NOT_PARAM));
        }
    }
}
